#! /bin/sh
# PCP QA Test No. 583
# based on 519, caused pmie to dump core initially
# also #698718
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

eval `./getpmcdhosts -n 2 -L | sed -e 's/ / other2=/' -e 's/^/other1=/'`
if [ -z "$other1" ]
then
    _notrun "Cannot find first remote host running pmcd"
    # NOTREACHED
fi
if [ -z "$other2" ]
then
    _notrun "Cannot find second remote host running pmcd"
    # NOTREACHED
fi
echo "other1=$other1" >>$seq_full
echo "other2=$other2" >>$seq_full

case $PCP_PLATFORM
in
    linux|solaris)
	# same syslog options for Linux and OpenSolaris
	SYSLOG_OPT='"-p daemon.info"'
	;;
    darwin)
	SYSLOG_OPT=''
	;;
    *)
	echo "Arrgh ... need syslog option for $PCP_PLATFORM"
	exit 1
	;;
esac

_filter()
{
    # other syslog timestamp variants ...
    # 2014-09-15T10:44:40.269104+10:00
    _filter_slow_pmie \
    | sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z] *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/TIMESTAMP/' \
	-e 's/[A-Z][a-z][a-z] *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/TIMESTAMP/' \
	-e 's/20[0-9][0-9]-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]*/TIMESTAMP/' \
	-e 's/STAMP[-+][0-1][0-9]:[0-5][0-9]/STAMP/' \
	-e "s/`hostname`/HOST/" \
	-e "s/`hostname | sed -e 's/\..*//'` /HOST /" \
	-e 's/[0-9][A-Z]:HOST/HOST/' \
	-e 's/\(pmie[([]\)[0-9][0-9]*/\1PID/'
}

_cleanup()
{
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    _restore_auto_restart pmlogger

    $sudo rm -f $tmp.*
}

signal=$PCP_BINADM_DIR/pmsignal
status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd
_stop_auto_restart pmlogger

PMCD_CONNECT_TIMEOUT=60
PMCD_REQUEST_TIMEOUT=60
PMCD_RECONNECT_TIMEOUT=1,2
export PMCD_CONNECT_TIMEOUT PMCD_REQUEST_TIMEOUT PMCD_RECONNECT_TIMEOUT

# may be using journalctl, and so no syslog in the file system
#
if [ "$PCPQA_SYSTEMD" = yes ] && `which journalctl >/dev/null 2>&1`
then
    echo "Using journalctl ..." >>$seq_full
    have_journalctl=true
else
    have_journalctl=false
    SYSLOG=''
    for f in /var/adm/SYSLOG /var/log/daemon.log /var/log/messages \
	     /var/log/syslog /var/log/syslog.1 /var/log/system.log
    do
	if [ -f $f -a -s $f ]
	then
	    SYSLOG=$f
	    break
	fi
    done
    if [ -z "$SYSLOG" ]
    then
	echo "Arrgh! Cannot find your syslog file"
	exit 1
    fi
fi

# real QA test starts here
$sudo rm -f core* $seq.core*

if ! _service pmcd stop 2>&1; then _exit 1; fi | _filter_pcp_start
if ! _service pmlogger stop; then _exit 1; fi \
| _filter_pcp_start
_wait_pmcd_end || _exit 1

# note the following is time sensitive, run it too long
# and we'll get extraneous output...
cat <<EOF >$tmp.in
// use hosts not in QA circus
//

hosts = ":'$other1' :'$other2' :localhost";

some_host (
  some_inst (
    ( 100 * filesys.used \$hosts / filesys.capacity \$hosts ) > 0 ) )
-> print 5sec "full" " %h:[%i] %v% "
 & syslog 5sec $SYSLOG_OPT "PCP QA $seq - please ignore - ..." " %h:[%i] %v% ";

EOF

echo "=== pmie.conf ===" >$seq_full
cat $tmp.in >>$seq_full

pmie -t 1sec >$tmp.out 2>$tmp.err <$tmp.in &
pmie_pid=$!

sleep 5
if ! _service pmcd start 2>&1; then _exit 1; fi | _filter_pcp_start
_wait_for_pmcd || _exit 1
if ! _service pmlogger start; then _exit 1; fi \
| _filter_pcp_start
_wait_for_pmlogger || _exit 1

sleep 15
$signal -s TERM $pmie_pid
wait

# hostnames $other1 and $other2 may contain domain name components,
# but pmie may just report the hostname
#
o1=`echo $other1 | sed -e 's/\..*//'`
o2=`echo $other2 | sed -e 's/\..*//'`
local=`hostname`
l=`echo $local | sed -e 's/\..*//'`
echo "o1=$o1" >>$seq_full
echo "o2=$o2" >>$seq_full
echo "local=$local" >>$seq_full
echo "l=$l" >>$seq_full
echo "pmie output ..."
sed <$tmp.out \
    -e "/ $other1:/d" \
    -e "/ $o1:/d" \
    -e "/ $other2:/d" \
    -e "/ $o2:/d" \
    -e '/ localhost:/d' \
    -e "/ $local:/d" \
    -e "/ $l:/d" \
#
grep -E " ($other1|$o1):" <$tmp.out >/dev/null && echo "... at least one line for other1 host"
grep -E " ($other2|$o2):" <$tmp.out >/dev/null && echo "... at least one line for other2 host"
grep -E " (localhost|$local|$l):" <$tmp.out >/dev/null && echo "... at least one line for localhost"

echo
echo "pmie stderr ..."
_filter <$tmp.err

echo >>$seq_full
echo "=== pmie stdout ===" >>$seq_full
cat $tmp.out >>$seq_full
echo >>$seq_full
echo "=== pmie stderr ===" >>$seq_full
cat $tmp.err >>$seq_full

echo
echo "SYSLOG ..."
if $have_journalctl
then
    # be careful ... --boot not supported in all versions, -l not
    # supported in all versions
    $sudo journalctl -b --full | tail -200 >$tmp.syslog 2>>$seq_full
else
    $sudo tail -200 $SYSLOG >$tmp.syslog
fi
sed <$tmp.syslog -n \
    -e '/message repeated [1-9][0-9]* times/d' \
    -e '/PCP QA '"$seq"' - please ignore/s/\.\.\..*/.../p' \
| _filter \
| LC_COLLATE=POSIX sort -u

echo >>$seq_full
if $have_journalctl
then
    echo "=== tail journalctl -b --full ===" >>$seq_full
else
    echo "=== tail SYSLOG ($SYSLOG) ===" >>$seq_full
fi
cat $tmp.syslog >>$seq_full

_check_core

exit
